package com.afkt.project.ui.slice;

import com.afkt.project.model.bean.MyListBean;
import com.afkt.project.model.bean.Test;
import com.afkt.project.ui.adapter.MyListAdapter;
import com.dev.utils.app.PathUtils;
import com.dev.utils.app.cache.WriteFileUtils;

import com.dev.utils.app.toast.ToastTintUtils;
import com.ohos.afkt.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.window.dialog.ToastDialog;
import ohos.utils.LruBuffer;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class DevCacheSlice extends AbilitySlice {

    private ListContainer listContainer;
    private MyListAdapter mAdapter;
    private Button goBack;
    private String path = PathUtils.getSDCard().getSDCardPath();

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_dev_cache);
        /**
         *设置返回
         * */
        goBack = (Button) findComponentById(ResourceTable.Id_btn_crash_back);
        if (goBack != null) {
            goBack.setClickedListener(component -> {
                onBackPressed();
            });
        }

        initList();
    }

    private void initList() {
        List<MyListBean> list = initData();
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_ringtone_list);
        mAdapter = new MyListAdapter(list, this);
        listContainer.setItemProvider(mAdapter);
        LruBuffer lruBuffer = new LruBuffer();
        listContainer.setItemClickedListener((listContainer, component, position, l) -> {
            switch (position) {
                //存储字符串
                case 0:
                    lruBuffer.put("StringKey", "这是字符串");
                    new ToastDialog(getContext()).setText("存储字符串成功").show();
                    break;
                case 1:
                    lruBuffer.put("StringKey", "这是有效期3秒字符串");
                    new ToastDialog(getContext()).setText("存储有效期字符串成功").show();
                    new Timer().schedule(new TimerTask() {
                        @Override
                        public void run() {
                            lruBuffer.clear();
                        }
                    }, 3000);
                    break;
                case 2:
                    if (lruBuffer.get("StringKey") == null) {
                        ToastTintUtils.error("未存储key为str的字符串");
                    } else {
                        new ToastDialog(getContext()).setText(lruBuffer.get("StringKey") + "").show();
                    }
                    break;
                //存储实体类
                case 3:
                    Test test = new Test();
                    test.setName("这是实体类");
                    test.setTime(System.currentTimeMillis() + "");
                    lruBuffer.put("entityKey", test);
                    new ToastDialog(getContext()).setText("存储实体类成功").show();
                    break;
                //存储有效期实体类
                case 4:
                    Test test1 = new Test();
                    test1.setName("这是3秒有效期实体类");
                    test1.setTime(System.currentTimeMillis() + "");
                    lruBuffer.put("entityKey", test1);
                    new Timer().schedule(new TimerTask() {
                        @Override
                        public void run() {
                            lruBuffer.clear();
                        }
                    }, 3000);
                    new ToastDialog(getContext()).setText("存储有效期实体类成功") .show();
                    break;
                //获取实体类
                case 5:
                    Object obj = lruBuffer.get("entityKey");
                    if (obj == null) {
                        ToastTintUtils.error("未存储key为bean的实体类");
                    } else {
                        ToastTintUtils.normal(obj.toString(),null);
                    }
                    break;
                //存储到指定位置
                case 6:
                    WriteFileUtils.WriteStringToFile(path + "/a.txt", "这是指定位置字符串");
                    new ToastDialog(getContext()).setText("存储到指定位置成功").show();
                    break;
                //获取指定位置缓存数据
                case 7:
                    String readFile = WriteFileUtils.readFile(path + "/a.txt");
                    if(readFile == null || readFile == ""){
                        ToastTintUtils.error("未存储key为fileStr的字符串");
                    }else{
                        new ToastDialog(getContext()).setText(readFile).show();
                    }
                    break;
                //清除全部数据
                case 8:
                    lruBuffer.clear();
                    new ToastDialog(getContext()).setText("清除全部数据成功").show();
                    break;
                default:
                    break;
            }
        });
    }

    private List<MyListBean> initData() {
        List<MyListBean> lists = new ArrayList<>();
        lists.add(new MyListBean("存储字符串"));
        lists.add(new MyListBean("存储有效期字符串"));
        lists.add(new MyListBean("获取字符串"));
        lists.add(new MyListBean("存储实体类"));
        lists.add(new MyListBean("存储有效期实体类"));
        lists.add(new MyListBean("获取实体类"));
        lists.add(new MyListBean("存储到指定位置"));
        lists.add(new MyListBean("获取指定位置缓存数据"));
        lists.add(new MyListBean("清除全部数据"));
        return lists;
    }
}
